#include "raytracer.h"

/*-<==>-----------------------------------------------------------------
/ 
/---------------------------------------------------------------------*/
CCamera::CCamera() {
	// Initialize with some default parameters
  setRenderParameters(320, 240, 60.0f);
	setView(VECTOR(0,0,0), VECTOR(1,0,0));
}

CCamera::~CCamera() {
}

/*-<==>-----------------------------------------------------------------
/ Save render parameters for later use
/ fov is in degrees
/---------------------------------------------------------------------*/
void CCamera::setRenderParameters (int axres, int ayres, SCALAR afov_in_deg) {
	xres = axres;
	yres = ayres;
	fov=afov_in_deg;
	SCALAR tan_fov = tan((afov_in_deg*M_PI/180)/2);
	viewd = (ayres/2)/tan_fov;	
}

/*-<==>-----------------------------------------------------------------
/ Define the axis of the camera (front, up, left) in world coordinates 
/ based on the current values of the vectors target & loc 
/---------------------------------------------------------------------*/
void CCamera::initAxis() {
  front = target-loc;
  front.normalize();
  VECTOR aux_up = VECTOR(0,1,0);
  left=aux_up.cross(front);
  aux_up.normalize();
  up = front.cross(left);
}

/*-<==>-----------------------------------------------------------------
/ Save the new camera position and target point
/---------------------------------------------------------------------*/
void CCamera::setView(const VECTOR &src_point, const VECTOR &dst_point) {
  loc = src_point;
  target = dst_point;
  initAxis();
}

/*-<==>-----------------------------------------------------------------
/ return a line which starts on camera position and goes through the pixel
/ (x,y) from the screen
/---------------------------------------------------------------------*/
CLine CCamera::getLineAt (SCALAR x, SCALAR y) const {
  VECTOR po = loc;
  SCALAR tmp_x_res= xres / 2 - x - 0.5;
  SCALAR tmp_y_res= yres / 2 - y - 0.5;
  VECTOR v_xy=front*viewd + left * tmp_x_res + up * tmp_y_res;
  return CLine(loc,v_xy, 0);
}

